import { routerRedux } from 'dva/router';
// import { stringify } from 'qs';
import { login, logout, getByToken } from '@/services/login';
import {
  getToken, setToken, removeToken, getUser, getRight, setUser, setRight, removeUser, removeRight
} from '@/utils/loginInfo';
import { message } from 'antd';
// import { getPageQuery } from '@/utils/utils';

export default {
  namespace: 'login',

  state: {
    status: false,
    token: null,
    currentUser: null,
    authority: [],
  },

  effects: {
    *getByToken({ payload }, { call, put }) {
      const token = yield getToken();
      // 是否登录
      if (!token) {
        // 否：跳转登录
        yield put(routerRedux.replace('/login'));
        // 更新状态
        yield [
          removeToken(),
          put({
            type: 'save',
            payload: { status: false, token: null, currentUser: null, authority: [] },
          }),
        ];
      } else {
        // 是：
        let user = yield getUser();
        let right = yield getRight();
        // 登录信息是否已存在
        if (!user || !right) {
          // 否，服务器获取
          const response = yield call(getByToken, token);
          if (response && response.code === 200) {
            user = response.data.user;
            right = response.data.right;
            // 存入SessionStorage
            yield [
              setUser(user),
              setRight(right),
            ]
          } else {
            // token过期、无效
            yield put(routerRedux.replace('/login'));
            // 更新状态
            yield [
              removeToken(),
              put({
                type: 'save',
                payload: { status: false, token: null, currentUser: null, authority: [] },
              }),
            ];
          }
        }
        // 更新状态
        yield put({
          type: 'save',
          payload: { status: true, token: token, currentUser: user, authority: right },
        });
        // 重新组织菜单信息
        yield put({
          type: 'menu/getMenuData',
          payload: { ...payload, authority: right },
        });
      }
    },

    *login({ payload }, { call, put }) {
      const response = yield call(login, payload);
      // Login successfully
      if (response && response.code === 200) {
        // 保存Token，默认：1天，记住我：14天
        yield setToken(response.data.token, payload.remember);
        // 存入SessionStorage
        yield [
          setUser(response.data.user),
          setRight(response.data.right),
        ]
        yield put({
          type: 'save',
          payload: { status: true, token: response.data.token, currentUser: response.data.user, authority: response.data.right },
        });
        yield put(routerRedux.replace('/'));
      } else {
        // 登陆失败的处理
        if (response && response.message) {
          message.error(response.message);
        } else {
          message.error('登陆失败');
        }
      }
    },

    *logout(_, { call, put }) {
      yield put(routerRedux.replace('/login'));
      yield call(logout, { token: getToken() });
      yield [
        removeToken(),
        removeUser(),
        removeRight(),
        put({
          type: 'save',
          payload: { status: false, token: null, currentUser: null, authority: [] },
        }),
      ];
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
